<template>
  <!-- 订单列表组件 -->
  <div class="order-container">
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片试图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="10">
        <el-col :span="10">
          <el-input
            clearable
            placeholder="请输入内容"
            class="searchInp"
            v-model="queryInfo.query"
            @clear="getOrderList()"
            @keyup.enter.native="getOrderList()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              class="searchBtn"
              @click="getOrderList()"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表区域 -->
      <el-table :data="orderList" height="calc(78%)" size="mini" border stripe>
        <!-- 序号 -->
        <el-table-column
          type="index"
          label="序号"
          align="center"
          header-align="center"
        ></el-table-column>
        <!-- 订单号 -->
        <el-table-column
          prop="order_number"
          label="订单编号"
          header-align="center"
          align="left"
          sortable
        ></el-table-column>
        <!-- 订单价格 -->
        <el-table-column
          prop="order_price"
          label="订单价格"
          align="center"
          header-align="center"
          sortable
        ></el-table-column>
        <!-- 是否付款 -->
        <el-table-column
          prop="order_pay"
          label="是否付款"
          align="center"
          header-align="center"
          sortable
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.order_pay === '0'" type="danger"
              >未付款</el-tag
            >
            <el-tag v-else type="success">已付款</el-tag>
          </template>
        </el-table-column>
        <!-- 是否发货 -->
        <el-table-column
          prop="is_send"
          label="是否发货"
          align="center"
          header-align="center"
          sortable
        ></el-table-column>
        <!-- 下单时间 -->
        <el-table-column
          label="下单时间"
          align="center"
          header-align="center"
          sortable
        >
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat('yyyy-MM-dd hh:mm:ss') }}
          </template>
        </el-table-column>
        <!-- 修改时间 -->
        <el-table-column
          label="修改时间"
          align="center"
          header-align="center"
          sortable
        >
          <template slot-scope="scope">
            {{ scope.row.update_time | dateFormat('yyyy-MM-dd hh:mm:ss') }}
          </template></el-table-column
        >
        <!-- 操作 -->
        <el-table-column
          label="操作"
          width="150px"
          align="center"
          header-align="center"
        >
          <template slot-scope="scope">
            <!-- 修改地址 -->
            <el-tooltip content="修改地址" placement="top" :enterable="false"
              ><el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="showAddressDetails(scope.row.order_id)"
              ></el-button
            ></el-tooltip>
            <!-- 物流信息 -->
            <el-tooltip content="物流信息" placement="top" :enterable="false">
              <el-button
                size="mini"
                type="success"
                icon="el-icon-location-outline"
                @click="showProgressDetails(scope.row.order_id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页条区域 -->
      <!-- 
          @size-change：pageSize（页面显示条数） 改变时会触发
          @current-change：currentPage（页码值） 改变时会触发
          current-page：默认显示第几页
          page-sizes：每页显示个数选择器的选项设置
          page-size：每页显示条目个数
          total：总条目数
          layout：组件布局，子组件名用逗号分隔
       -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryInfo.pagesize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>

      <!-- 修改地址对话框 -->
      <el-dialog
        title="修改地址"
        :visible.sync="addressDialogVisible"
        width="40%"
        @close="addressDialogClose"
      >
        <el-form
          :model="addressForm"
          :rules="addressRules"
          ref="addressRef"
          label-width="100px"
        >
          <el-form-item label="省市区/县" prop="address1">
            <!-- cascader
               change-on-select
               clearable:是否支持清空选项
               options:可选项数据源，键名可通过 Props 属性配置
               props:配置选项 
               @change:当选中节点变化时触发
           -->
            <el-cascader
              clearable
              ref="cascaderRef"
              v-model="addressForm.address1"
              :options="cityData"
              :props="cascaderProps"
              @change="parendCateChange"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="addressForm.address2"></el-input>
          </el-form-item>
        </el-form>

        <!-- 底部区域 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="addressDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addressDialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>

      <!-- 物流信息对话框 -->
      <el-dialog
        title="物流信息"
        :visible.sync="progressDialogVisible"
        width="40%"
      >
        <!-- 时间线 -->
        <!-- el-timeline-item
            timestamp:时间戳
            placement:时间戳位置(top / bottom)
            type:节点类型(primary / success / warning / danger / info)
            color:颜色(	hsl / hsv / hex / rgb)
            size:节点尺寸(normal / large)
            icon:图标
         -->
        <el-timeline>
          <el-timeline-item
            v-for="(activity, index) in progressInfo"
            :key="index"
            :timestamp="activity.time"
            icon="el-icon-check"
            type="primary"
            size="large"
          >
            {{ activity.context }}
          </el-timeline-item>
        </el-timeline>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
// 导入城市区县数据
import cityData from './citydata'

export default {
  data() {
    return {
      // 订单数据列表
      orderList: [],
      // 订单查询对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
        user_id: '',
        pay_status: '',
        is_send: '',
        order_fapiao_title: '',
        order_fapiao_company: '',
        order_fapiao_content: '',
        consignee_addr: ''
      },
      total: 0,
      addressDialogVisible: false,
      progressDialogVisible: false,
      addressForm: {
        address1: [],
        address2: ''
      },
      addressRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 城市区县数据
      cityData,
      // 级联选择器的配置对象
      cascaderProps: {
        value: 'value',
        label: 'label',
        children: 'children',
        // 次级菜单的展开方式
        expandTrigger: 'hover',
        // 是否严格的遵守父子节点不互相关联(可以选择任意一级)
        checkStrictly: false
      },
      // 物流信息列表
      progressInfo: []
    }
  },
  created() {
    // 获取订单列表
    this.getOrderList()
  },
  methods: {
    // 获取订单列表
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200)
        return this.$message.error('获取订单列表失败！')
      this.total = res.data.total
      this.orderList = res.data.goods
    },
    // 监听 pageSize（页面显示条数） 改变的事件
    handleSizeChange(newSize) {
      // newSize：新的页面显示条数
      this.queryInfo.pagesize = newSize
      // 重新获取用户列表数据
      this.getOrderList()
    },
    // 监听 pagenum（页码值） 改变的事件
    handleCurrentChange(newPage) {
      // newPage：新的页码值
      this.queryInfo.pagenum = newPage
      // 重新获取用户列表数据
      this.getOrderList()
    },
    // 显示修改地址对话框
    showAddressDetails(id) {
      console.log(id)
      this.addressDialogVisible = true
    },
    // 关闭修改地址对话框
    addressDialogClose() {
      // 重置表单
      this.$refs.addressRef.resetFields()
    },
    // 级联选择器选择项发生改变时触发函数
    parendCateChange() {
      // 隐藏级联选择器的下拉选择列表
      this.$refs.cascaderRef.dropDownVisible = false
    },
    // 显示物流进度对话框
    async showProgressDetails(id) {
      console.log(id)
      const { data: res } = await this.$http.get('/kuaidi/' + 1106975712662)
      if (res.meta.status !== 200)
        return this.$message.error('获取物流信息失败！')
      this.progressInfo = res.data
      this.progressDialogVisible = true
    }
  },
  computed: {},
  watch: {},
  filters: {}
}
</script>

<style lang="less" scoped>
.order-container {
  height: 100%;
  max-height: 100%;
  overflow: auto;

  // 搜索框
  .searchInp {
    outline: none !important;
    border: none !important;
  }

  // 搜索按钮
  .searchBtn {
    background-color: #409eff;
    outline: none;
    border-radius: 3px;
    color: #fff;
    opacity: 0.8;

    &:hover {
      opacity: 0.6;
    }

    &:active {
      opacity: 1;
    }
  }
}
</style>
